import Constants from './../data/Constans';
import { _decorator, Component, Node, director, AudioSourceComponent, loader, AudioClip } from "cc";
const { ccclass, property } = _decorator;

// 事例：
// Constants.OnFire.emit(Constants.EventName.AUDIO_STATUS, { key: 'button' })

@ccclass("AudioManager")
export class AudioManager extends Component {
    // 音效组件
    private _audioSourceComponent = null;

    // BGM音效组件
    private _bgmComponent = null;

    // 魅族特有对象
    // private _InnerAudioContext = null;

    // 声音管理策略
    private _audioStrategies = {
        // BGM
        'bgm': () => { this._bgmComponent.play(); },

        // 蚊子死亡音效
        'enemy': () => { this._playAudio('enemy'); },

        // 碰撞障碍物音效
        'obstacle': () => { this._playAudio('obstacle'); },

        // 倒计时音效
        'countdown': () => { this._playAudio('countdown'); },

        // 按钮音效
        'button': () => { this._playAudio('button'); },

        // 金币获取音效
        'gold': () => { this._playAudio('gold'); },

        // 轻提示音效
        'tips': () => { this._playAudio('tips'); },

        // 胜利
        'pass': () => { this._playAudio('pass'); },

        // 礼花爆炸
        'bomb': () => { this._playAudio('bomb'); },

        // 金币音效（游戏中）
        'glod_1': () => { this._playAudio('glod_1'); },

        // 获取道具或升级头衔
        'prop': () => { this._playAudio('prop'); },

        // 出发
        'go': () => { this._playAudio('go'); },

        // 出发
        'readygo': () => { this._playAudio('readygo'); },

        // 发射武器
        'launch': () => { this._playAudio('launch'); },

        // 伤害
        'hurt': () => { this._playAudio('hurt'); },

        // 一血
        'kill_1': () => { this._playAudio('firstblood'); },

        // 二血
        'kill_2': () => { this._playAudio('doublekill'); },

        // 三血
        'kill_3': () => { this._playAudio('triplekill'); },

        // 四血
        'kill_4': () => { this._playAudio('quadrakill'); },

        // 五血
        'kill_5': () => { this._playAudio('pentakill'); },

        // 掉落
        'help': () => { this._playAudio('help'); },
    }

    onLoad() {
        // 声音监听器
        Constants.OnFire.on(Constants.EventName.AUDIO_STATUS, this._audioManaga.bind(this));
    }

    start() {
        // 获取音效组件（注意：只针对第一个AudioSourceComponent组件）
        let _audios = this.node.getComponents(AudioSourceComponent);
        this._audioSourceComponent = _audios[0];
        this._bgmComponent = _audios[1];
    }

    // 声音控制
    private _audioManaga(event) {
        this._audioStrategies[event.key]();
    }

    // 播放音效
    private _playAudio(audio_name) {
        let url = 'bgm/' + audio_name;
        loader.loadRes(url, AudioClip, (err, clip) => {
            if (err) {
                printLog('播放失败', err);
                return;
            };

            // 加载音效clip并播放
            this._audioSourceComponent.clip = clip;
            this._audioSourceComponent.play();
        });
    }
}
